package com.ruoyi.ora.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpRequest;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.ora.domain.VoiceRecord;
import com.ruoyi.ora.domain.VoiceTranModel;
import com.ruoyi.ora.service.IVoiceRecordService;
import com.ruoyi.ora.service.IVoiceTranslatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;

import java.net.URLEncoder;

public class VoiceTranslatWhisperServiceImpl implements IVoiceTranslatService {

    private static final Logger logger = LoggerFactory.getLogger(VoiceTranslatWhisperServiceImpl.class);

    private IVoiceRecordService voiceRecordService = SpringUtils.getBean(IVoiceRecordService.class);

    @Override
    public VoiceTranModel voiceTranslat(String voicePath) {
        return null;
    }

    @Override
    public AjaxResult voiceTranslat(VoiceRecord voiceRecord) {

        try {
            /**
             * 转写服务器信息
             */
//            String ip = "45.204.146.75";
//            String username = "root";
//            String password = "e4%qR1x5@Ca6Wb";
//            String workPath = "/home/voice";
//
////            sshpass -p 'e4%qR1x5@Ca6Wb' ssh -p 22   -o StrictHostKeyChecking=no   root@45.204.146.75   "/bin/bash -c '/bin/mkdir -p /home/voice/profile/upload/2025/04/25'"
//
//            // 先把文件移到目录服务器
//            String dirPath = voiceRecord.getVoicePath().substring(0, voiceRecord.getVoicePath().lastIndexOf("/"));
//            CurrencyUtil.execCmd("sshpass -p '"+password+"' ssh -p 22 -o StrictHostKeyChecking=no \""+username+"@"+ip+"\" \"mkdir -p "+workPath+dirPath+"\"");
////            CurrencyUtil.remoteExecCmd(ip, username, password, "mkdir -p "+workPath+dirPath);
//            String filePath = RuoYiConfig.getProfile()+voiceRecord.getVoicePath().replace(Constants.RESOURCE_PREFIX, "");
//            CurrencyUtil.execCmd("sshpass -p '"+password+"' scp -P 22 -o StrictHostKeyChecking=no -r "+filePath+" "+username+"@"+ip+":"+workPath+voiceRecord.getVoicePath());
//
//
//            String result = CurrencyUtil.remoteExecCmd(ip, username, password, "python3 /home/test/q.py " + workPath + voiceRecord.getVoicePath(), "utf-8");


//            String result = CurrencyUtil.execCmd("python3 /home/work/whisper/q.py " + voiceRecord.getVoicePath().replace(Constants.RESOURCE_PREFIX, RuoYiConfig.getProfile()));

            HttpRequest request = new HttpRequest();
            String result = request.sendFile("http://218.17.184.249:55000/transcribe", new FileSystemResource(voiceRecord.getVoicePath().replace(Constants.RESOURCE_PREFIX, RuoYiConfig.getProfile())));

            logger.info("Voice记录id【"+voiceRecord.getId()+"】，转写结果："+result);

            VoiceTranModel voiceTranModel = JSONObject.parseObject(result, VoiceTranModel.class);
            voiceRecordService.tranFinishHandle(voiceRecord, voiceTranModel);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @Override
    public VoiceTranModel textTranVoice(String content, String voicePath) {
        return null;
    }

    @Override
    public AjaxResult realVoiceTranslat() {
//        AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE);
//        recorder.startRecording();
//
//        byte[] audioData = new byte[BUFFER_SIZE];
//        recorder.read(audioData, 0, BUFFER_SIZE);
//
//        WhisperEngine engine = new WhisperEngine();
//        String result = engine.convertAudioToText(audioData);



        return null;
    }

    public static void main(String[] args) throws Exception{
        String a = "/profile/upload/2024/12/04/recording_1733280911425_20241204105539A004.m4a";
        System.out.println(a.substring(0, a.lastIndexOf("/")));


        String result = "";
//            String result = CurrencyUtil.execCmd("python3 /home/work/whisper/q.py " + voiceRecord.getVoicePath().replace(Constants.RESOURCE_PREFIX, RuoYiConfig.getProfile()));



//
//        HttpRequest request = new HttpRequest();
//        File file = new File("D://123.silk");
//        HttpRespons httpRespons = request.sendPost("http://218.17.184.249:55000/transcribe", file);
//        if (httpRespons != null && StringUtils.isNotBlank(httpRespons.getContent())) {
//            System.out.println(httpRespons.getContent());
//        }

        HttpRequest request = new HttpRequest();
        String re111 = request.sendFile("http://218.17.184.249:55000/transcribe", new FileSystemResource("D://123.silk"));


        System.out.println(re111);


        re111 = URLEncoder.encode("语音转文字结果", "utf-8");
        System.out.println(re111);




    }

}
